﻿/*
 * File: GamePlayBaseScene_HandleInput.cs
 * Author: Gourav Das
 * Purpose: Input handle for gameplay
 * Created: June 17 2011
 */

using System;
using System.Linq;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Threading;
using System.IO;
using _22yards._2D;
using _22yards.Scenes.Helpers;
using _22yards.Core;
using _22yards;
using _22yards.EnumDeclarations;
using _22yards.ThreeD;
using _22yards.Helpers;
using Komires.MataliPhysics;

namespace _22yards.Scenes
{
	/// <summary>
	/// This is a scene that implements the 2D Gameplay screen
	/// </summary>
	partial class GamePlayBaseScene : Scene
	{
		/// <summary>
		/// Lets the game respond to player input. Unlike the Update method,
		/// this will only be called when the gameplay scene is active.
		/// </summary>
		public override void HandleInput(InputEngine _input)
		{
			if (_input == null)
				throw new ArgumentNullException("null input");

			// Look up inputs for the active player profile.
			int playerIndex = (int)ControllingPlayer.Value;
			PlayerIndex plidx;

			KeyboardState keyboardState = _input.CurrentKeyboardStates[playerIndex];
			GamePadState gamePadState = _input.CurrentGamePadStates[playerIndex];
			MouseState mouseState = _input.CurrentMouseStates;

			KeyboardState lastkeyboardState = _input.LastKeyboardStates[playerIndex];

			// The game pauses either if the user presses the pause button, or if
			// they unplug the active gamepad. This requires us to keep track of
			// whether a gamepad was ever plugged in, because we don't want to pause
			// on PC if they are playing with a keyboard and have no gamepad at all!
			bool gamePadDisconnected = !gamePadState.IsConnected &&
									   _input.GamePadWasConnected[playerIndex];

			if(lastkeyboardState.IsKeyDown(Keys.LeftControl) && (keyboardState.IsKeyUp(Keys.D2) && lastkeyboardState.IsKeyDown(Keys.D2)))
			{
				eGamePlayType = (short)GamePlayType.TwoD;
			}
			else if (lastkeyboardState.IsKeyDown(Keys.LeftControl) && (keyboardState.IsKeyUp(Keys.D3) && lastkeyboardState.IsKeyDown(Keys.D3)))
			{
				eGamePlayType = (short)GamePlayType.ThreeD;
			}

			if (_input.IsPauseGame(ControllingPlayer) || gamePadDisconnected)
			{
				sceneEngine.AddScene(new PauseScene(), ControllingPlayer);
			}
			else if (eGamePlayType == (short)GamePlayType.TwoD)
			{

				if (keyboardState.IsKeyUp(Keys.R) && lastkeyboardState.IsKeyDown(Keys.R))
				{
					ReliefMapping = !ReliefMapping;
				}

				if (lastkeyboardState.IsKeyDown(Keys.H))
				{
					rightSideBarModel.HeightScale += 0.001f;
				}
				if (lastkeyboardState.IsKeyDown(Keys.J))
				{
					rightSideBarModel.HeightScale -= 0.001f;
				}

				if (_input.IsMouseLeftButtonPress(ControllingPlayer, out plidx))
				{
					DblClickTimeBomb = true;
					click1 = true;
				}

				if ((BallObject.IsSleeping || KBRedBall.Position.Y < -15f) && (keyboardState.IsKeyUp(Keys.Enter) && lastkeyboardState.IsKeyDown(Keys.Enter)))
				{
					birdEyeViewChaseCamera.ResetCamera();
					FireBall();
				}
				else if ((keyboardState.IsKeyUp(Keys.Enter) && lastkeyboardState.IsKeyDown(Keys.Enter)))
				{
					PausePhysics = !PausePhysics;
				}
				
				if (keyboardState.IsKeyDown(Keys.I))
				{
					if (iRightSidebarStartheight > iRightSidebarEndheight && iInfoBarStartwidth < iInfobarEndWidth)
						bStartSidebarShrinkingTransition = true;

					if (iRightSidebarStartheight < iRightSidebarEndheight && iInfoBarStartwidth > iInfobarEndWidth)
						bStartInfobarShrinkTransition = true;
				}

				if (keyboardState.IsKeyDown(Keys.F1))
				{
					if (DisplayRemainingBatsMan || DisplayRemainingBowler)
					{
						DisplayRemainingBatsMan = false;
						DisplayRemainingBowler = false;
						FirstColCurrentXPos = -50;
						SecondColCurrentXPos = -50;
						ThirdColCurrentXPos = -50;

						for (int i = 4; i < 22; i++)
							photoFrames[i].Position.X = -50f;

						FirstColCurrentXPos = -30;
						SecondColCurrentXPos = -30;
						ThirdColCurrentXPos = -30;
						for (int i = 0; i < 4; i++)
							photoFrames[i].IsMouseLeftClickSelected = false;

						if (photoFrames[0].Oscillate == false)
						{
							photoFrames[0].SyncOscillationsWithModel(photoFrames[1]);
							photoFrames[0].Oscillate = true;
						}
						if (photoFrames[1].Oscillate == false)
						{
							photoFrames[1].SyncOscillationsWithModel(photoFrames[0]);
							photoFrames[1].Oscillate = true;
						}
						if (photoFrames[2].Oscillate == false)
						{
							photoFrames[2].SyncOscillationsWithModel(photoFrames[3]);
							photoFrames[2].Oscillate = true;
						}
						if (photoFrames[3].Oscillate == false)
						{
							photoFrames[3].SyncOscillationsWithModel(photoFrames[2]);
							photoFrames[3].Oscillate = true;
						}

						TransitionTimeBomb = true;
						TransitionelapsedTime = TimeSpan.FromSeconds(1.2f);
						if (SubScreenState != (short)TwoDEESubScreenState.Summary)
						{
							SubScreenState = (short)TwoDEESubScreenState.Summary;
							TextPrintedOnTexture = false;
						}
					}

					if (bShowField)
					{
						TransitionTimeBomb = true;
						TransitionelapsedTime = TimeSpan.FromSeconds(1.2f);
						if (SubScreenState != (short)TwoDEESubScreenState.Summary)
						{
							SubScreenState = (short)TwoDEESubScreenState.Summary;
							TextPrintedOnTexture = false;
						}
						bMobileAppear = true;
					}

					if (SubScreenState != (short)TwoDEESubScreenState.Summary && (!rightSideBarModel.transitionOut && !rightSideBarModel.transitionIn))
					{
						if (!TransitionTimeBomb)
						{
							SubScreenState = (short)TwoDEESubScreenState.Summary;
							rightSideBarModel.transitionOut = true;
							rightSideBarModel.Oscillate = false;
							TransitionTimeBomb = true;
							TextPrintedOnTexture = false;
						}
					}
				}

				if (keyboardState.IsKeyDown(Keys.F2))
				{
					if (DisplayRemainingBatsMan || DisplayRemainingBowler)
					{
						DisplayRemainingBatsMan = false;
						DisplayRemainingBowler = false;
						FirstColCurrentXPos = -50;
						SecondColCurrentXPos = -50;
						ThirdColCurrentXPos = -50;

						for (int i = 4; i < 22; i++)
							photoFrames[i].Position.X = -50f;

						for (int i = 0; i < 4; i++)
							photoFrames[i].IsMouseLeftClickSelected = false;

						if (photoFrames[0].Oscillate == false)
						{
							photoFrames[0].SyncOscillationsWithModel(photoFrames[1]);
							photoFrames[0].Oscillate = true;
						}
						if (photoFrames[1].Oscillate == false)
						{
							photoFrames[1].SyncOscillationsWithModel(photoFrames[0]);
							photoFrames[1].Oscillate = true;
						}
						if (photoFrames[2].Oscillate == false)
						{
							photoFrames[2].SyncOscillationsWithModel(photoFrames[3]);
							photoFrames[2].Oscillate = true;
						}
						if (photoFrames[3].Oscillate == false)
						{
							photoFrames[3].SyncOscillationsWithModel(photoFrames[2]);
							photoFrames[3].Oscillate = true;
						}

						TransitionTimeBomb = true;
						TransitionelapsedTime = TimeSpan.FromSeconds(1.2f);
						if (SubScreenState != (short)TwoDEESubScreenState.ScoreCardView)
						{
							SubScreenState = (short)TwoDEESubScreenState.ScoreCardView;
							TextPrintedOnTexture = false;
						}
					}

					if (bShowField)
					{
						TransitionTimeBomb = true;
						TransitionelapsedTime = TimeSpan.FromSeconds(1.2f);
						if (SubScreenState != (short)TwoDEESubScreenState.ScoreCardView)
						{
							SubScreenState = (short)TwoDEESubScreenState.ScoreCardView;
							TextPrintedOnTexture = false;
						}
						bMobileAppear = true;
					}

					if (SubScreenState != (short)TwoDEESubScreenState.ScoreCardView && (!rightSideBarModel.transitionOut && !rightSideBarModel.transitionIn))
					{
						if (!TransitionTimeBomb)
						{
							SubScreenState = (short)TwoDEESubScreenState.ScoreCardView;
							rightSideBarModel.transitionOut = true;
							rightSideBarModel.Oscillate = false;
							TransitionTimeBomb = true;
							TextPrintedOnTexture = false;
						}
					}
				}

				if (keyboardState.IsKeyDown(Keys.F3))
				{
					if (DisplayRemainingBatsMan || DisplayRemainingBowler)
					{
						DisplayRemainingBatsMan = false;
						DisplayRemainingBowler = false;
						FirstColCurrentXPos = -50;
						SecondColCurrentXPos = -50;
						ThirdColCurrentXPos = -50;

						for (int i = 4; i < 22; i++)
							photoFrames[i].Position.X = -50f;

						for (int i = 0; i < 4; i++)
							photoFrames[i].IsMouseLeftClickSelected = false;

						if (photoFrames[0].Oscillate == false)
						{
							photoFrames[0].SyncOscillationsWithModel(photoFrames[1]);
							photoFrames[0].Oscillate = true;
						}
						if (photoFrames[1].Oscillate == false)
						{
							photoFrames[1].SyncOscillationsWithModel(photoFrames[0]);
							photoFrames[1].Oscillate = true;
						}
						if (photoFrames[2].Oscillate == false)
						{
							photoFrames[2].SyncOscillationsWithModel(photoFrames[3]);
							photoFrames[2].Oscillate = true;
						}
						if (photoFrames[3].Oscillate == false)
						{
							photoFrames[3].SyncOscillationsWithModel(photoFrames[2]);
							photoFrames[3].Oscillate = true;
						}

						if (SubScreenState != (short)TwoDEESubScreenState.BirdEyeView)
						{
							SubScreenState = (short)TwoDEESubScreenState.BirdEyeView;
						}
					}

					if (!bShowField)
					{
						bShowField = true;
						bMobileDissappear = true;
						SubScreenState = (short)TwoDEESubScreenState.BirdEyeView;
					}
				}

				if (keyboardState.IsKeyDown(Keys.D1))
				{
					bool exit = false;
					for (int i = 0; i < photoFrames.Count; i++)
					{
						if (photoFrames[i].IsMouseLeftClickSelected)
						{
							exit = true;
							break;
						}
					}

					if (!exit)
					{
						photoFrames[0].IsMouseLeftClickSelected = true;
					}
				}
				if (lastkeyboardState.IsKeyUp(Keys.LeftControl) && keyboardState.IsKeyDown(Keys.D2))
				{
					bool exit = false;
					for (int i = 0; i < photoFrames.Count; i++)
					{
						if (photoFrames[i].IsMouseLeftClickSelected)
						{
							exit = true;
							break;
						}
					}

					if (!exit)
					{
						photoFrames[1].IsMouseLeftClickSelected = true;
					}
				}
				if (lastkeyboardState.IsKeyUp(Keys.LeftControl) && keyboardState.IsKeyDown(Keys.D3))
				{
					bool exit = false;
					for (int i = 0; i < photoFrames.Count; i++)
					{
						if (photoFrames[i].IsMouseLeftClickSelected)
						{
							exit = true;
							break;
						}
					}

					if (!exit)
					{
						photoFrames[2].IsMouseLeftClickSelected = true;
					}
				}
				if (keyboardState.IsKeyDown(Keys.D4))
				{
					bool exit = false;
					for (int i = 0; i < photoFrames.Count; i++)
					{
						if (photoFrames[i].IsMouseLeftClickSelected)
						{
							exit = true;
							break;
						}
					}

					if (!exit)
					{
						photoFrames[3].IsMouseLeftClickSelected = true;
					}
				}

				//If dbl click
				if (click1 && _input.IsMouseLeftButtonPress(ControllingPlayer, out plidx) && DblClickElapsedTimer > TimeSpan.FromMilliseconds(10) && DblClickElapsedTimer < TimeSpan.FromMilliseconds(DoubleClickDelay))
				{
					DblClickTimeBomb = false;
					click1 = false;
					Vector3 nearsource = new Vector3((float)mouseState.X, (float)mouseState.Y, 0f);
					Vector3 farsource = new Vector3((float)mouseState.X, (float)mouseState.Y, 49f);

					Matrix world = Matrix.CreateTranslation(0, 0, 0);

					Vector3 nearPoint = sceneEngine.GraphicsDevice.Viewport.Unproject(nearsource, staticCamera.ProjectionMatrix, staticCamera.ViewMatrix, world);

					Vector3 farPoint = sceneEngine.GraphicsDevice.Viewport.Unproject(farsource, staticCamera.ProjectionMatrix, staticCamera.ViewMatrix, world);
					Vector3 direction = farPoint - nearPoint;
					direction.Normalize();
					Ray pickRay = new Ray(nearPoint, direction);
					bool exit = false;
					for (int i = 0; i < photoFrames.Count; i++)
					{
						if (pickRay.Intersects(photoFrames[i].BoundingSphere) != null)
						{
							for (int j = 0; j < photoFrames.Count; j++)
							{
								if (photoFrames[j].IsMouseLeftDoubleClickSelected)
								{
									exit = true;
									break;
								}
							}

							if (exit)
								break;

							photoFrames[i].IsMouseLeftDoubleClickSelected = true;
							break;
						}

					}
				}

				//single click
				else if (click1 && DblClickElapsedTimer >= TimeSpan.FromMilliseconds(DoubleClickDelay))
				{
					DblClickTimeBomb = false;
					click1 = false;
					Vector3 nearsource = new Vector3((float)mouseState.X, (float)mouseState.Y, 0f);
					Vector3 farsource = new Vector3((float)mouseState.X, (float)mouseState.Y, 1f);

					Matrix world = Matrix.CreateTranslation(0, 0, 0);

					Vector3 nearPoint = sceneEngine.GraphicsDevice.Viewport.Unproject(nearsource, staticCamera.ProjectionMatrix, staticCamera.ViewMatrix, world);

					Vector3 farPoint = sceneEngine.GraphicsDevice.Viewport.Unproject(farsource, staticCamera.ProjectionMatrix, staticCamera.ViewMatrix, world);
					Vector3 direction = farPoint - nearPoint;
					direction.Normalize();
					Ray pickRay = new Ray(nearPoint, direction);
					bool exit = false;
					for (int i = 0; i < photoFrames.Count; i++)
					{
						if (pickRay.Intersects(photoFrames[i].BoundingSphere) != null)
						{
							for (int j = 0; j < photoFrames.Count; j++)
							{
								if (photoFrames[j].IsMouseLeftClickSelected)
								{
									exit = true;
									break;
								}
							}

							if (exit)
								break;

							photoFrames[i].IsMouseLeftClickSelected = true;
							break;
						}

					}
				}
			
			}
			else if (eGamePlayType == (short)GamePlayType.ThreeD)
			{
				//if (keyboardState.IsKeyUp(Keys.D9) && lastkeyboardState.IsKeyDown(Keys.D9))
				//{
				//    DepthBias += 0.001f;
				//}
				//if (keyboardState.IsKeyUp(Keys.D0) && lastkeyboardState.IsKeyDown(Keys.D0))
				//{
				//    DepthBias -= 0.001f;
				//}

				if (lastkeyboardState.IsKeyDown(Keys.H))
				{
					Pitch.HeightScale += 0.001f;
				}
				if (lastkeyboardState.IsKeyDown(Keys.J))
				{
					Pitch.HeightScale -= 0.001f;
				}
				if (keyboardState.IsKeyUp(Keys.B) && lastkeyboardState.IsKeyDown(Keys.B))
				{
					ShowBloom = !ShowBloom;
				}

				if (keyboardState.IsKeyUp(Keys.M) && lastkeyboardState.IsKeyDown(Keys.M))
				{
					ShowShadowMap = !ShowShadowMap;
				}

				if (keyboardState.IsKeyUp(Keys.R) && lastkeyboardState.IsKeyDown(Keys.R))
				{
					ReliefMapping = !ReliefMapping;
				}

				if (keyboardState.IsKeyUp(Keys.X) && lastkeyboardState.IsKeyDown(Keys.X))
				{
					ShowGaussianBlur = !ShowGaussianBlur;
				}

				if (keyboardState.IsKeyUp(Keys.Z) && lastkeyboardState.IsKeyDown(Keys.Z))
				{
					ShowShadows = !ShowShadows;
				}

				if (keyboardState.IsKeyUp(Keys.D1) && lastkeyboardState.IsKeyDown(Keys.D1))
				{
					ShowBoundingBoxes = !ShowBoundingBoxes;

					if (ShowBoundingBoxes)
					{
						Pitch.EnableDrawBoundingBox(true);
						LeftStumpFarEnd.EnableDrawBoundingBox(true);
						RightStumpFarEnd.EnableDrawBoundingBox(true);
						CenterStumpFarEnd.EnableDrawBoundingBox(true);
						LeftStumpNearEnd.EnableDrawBoundingBox(true);
						RightStumpNearEnd.EnableDrawBoundingBox(true);
						CenterStumpNearEnd.EnableDrawBoundingBox(true);
						LeftBailFarEnd.EnableDrawBoundingBox(true);
						RightBailFarEnd.EnableDrawBoundingBox(true);
						LeftBailNearEnd.EnableDrawBoundingBox(true);
						RightBailNearEnd.EnableDrawBoundingBox(true);
						stadia.EnableDrawBoundingBox(true);

						if (KBRedBall != null)
							KBRedBall.EnableDrawBoundingBox(true);
					}
					else
					{
						Pitch.EnableDrawBoundingBox(false);
						LeftStumpFarEnd.EnableDrawBoundingBox(false);
						RightStumpFarEnd.EnableDrawBoundingBox(false);
						CenterStumpFarEnd.EnableDrawBoundingBox(false);
						LeftStumpNearEnd.EnableDrawBoundingBox(false);
						RightStumpNearEnd.EnableDrawBoundingBox(false);
						CenterStumpNearEnd.EnableDrawBoundingBox(false);
						LeftBailFarEnd.EnableDrawBoundingBox(false);
						RightBailFarEnd.EnableDrawBoundingBox(false);
						LeftBailNearEnd.EnableDrawBoundingBox(false);
						RightBailNearEnd.EnableDrawBoundingBox(false);
						stadia.EnableDrawBoundingBox(false);
						if (KBRedBall != null)
							KBRedBall.EnableDrawBoundingBox(false);
					}
				
				}

				if (keyboardState.IsKeyUp(Keys.D2) && lastkeyboardState.IsKeyDown(Keys.D2))
				{
					ShowPhysicsObjects = !ShowPhysicsObjects;

					if (ShowPhysicsObjects)
					{
						Pitch.EnableDrawPhysicsObject(true);
						LeftStumpFarEnd.EnableDrawPhysicsObject(true);
						RightStumpFarEnd.EnableDrawPhysicsObject(true);
						CenterStumpFarEnd.EnableDrawPhysicsObject(true);
						LeftStumpNearEnd.EnableDrawPhysicsObject(true);
						RightStumpNearEnd.EnableDrawPhysicsObject(true);
						CenterStumpNearEnd.EnableDrawPhysicsObject(true);
						LeftBailFarEnd.EnableDrawPhysicsObject(true);
						RightBailFarEnd.EnableDrawPhysicsObject(true);
						LeftBailNearEnd.EnableDrawPhysicsObject(true);
						RightBailNearEnd.EnableDrawPhysicsObject(true);
						stadia.EnableDrawPhysicsObject(true);
						if (KBRedBall != null)
							KBRedBall.EnableDrawPhysicsObject(true);
					}
					else
					{
						Pitch.EnableDrawPhysicsObject(false);
						LeftStumpFarEnd.EnableDrawPhysicsObject(false);
						RightStumpFarEnd.EnableDrawPhysicsObject(false);
						CenterStumpFarEnd.EnableDrawPhysicsObject(false);
						LeftStumpNearEnd.EnableDrawPhysicsObject(false);
						RightStumpNearEnd.EnableDrawPhysicsObject(false);
						CenterStumpNearEnd.EnableDrawPhysicsObject(false);
						LeftBailFarEnd.EnableDrawPhysicsObject(false);
						RightBailFarEnd.EnableDrawPhysicsObject(false);
						LeftBailNearEnd.EnableDrawPhysicsObject(false);
						RightBailNearEnd.EnableDrawPhysicsObject(false);
						stadia.EnableDrawPhysicsObject(false);
						if (KBRedBall != null)
							KBRedBall.EnableDrawPhysicsObject(false);
					}

				}

				if (keyboardState.IsKeyUp(Keys.D4) && lastkeyboardState.IsKeyDown(Keys.D4))
				{
					ShowModels = !ShowModels;

					if (ShowModels)
					{
						Pitch.EnableDrawModel(true);
						LeftStumpFarEnd.EnableDrawModel(true);
						RightStumpFarEnd.EnableDrawModel(true);
						CenterStumpFarEnd.EnableDrawModel(true);
						LeftStumpNearEnd.EnableDrawModel(true);
						RightStumpNearEnd.EnableDrawModel(true);
						CenterStumpNearEnd.EnableDrawModel(true);
						LeftBailFarEnd.EnableDrawModel(true);
						RightBailFarEnd.EnableDrawModel(true);
						LeftBailNearEnd.EnableDrawModel(true);
						RightBailNearEnd.EnableDrawModel(true);
						stadia.EnableDrawModel(true);
						if (KBRedBall != null)
							KBRedBall.EnableDrawModel(true);
					}
					else
					{
						Pitch.EnableDrawModel(false);
						LeftStumpFarEnd.EnableDrawModel(false);
						RightStumpFarEnd.EnableDrawModel(false);
						CenterStumpFarEnd.EnableDrawModel(false);
						LeftStumpNearEnd.EnableDrawModel(false);
						RightStumpNearEnd.EnableDrawModel(false);
						CenterStumpNearEnd.EnableDrawModel(false);
						LeftBailFarEnd.EnableDrawModel(false);
						RightBailFarEnd.EnableDrawModel(false);
						LeftBailNearEnd.EnableDrawModel(false);
						RightBailNearEnd.EnableDrawModel(false);
						stadia.EnableDrawModel(false);
						if (KBRedBall != null)
							KBRedBall.EnableDrawModel(false);
					}

				}

				if (keyboardState.IsKeyDown(Keys.Up))
				{
					//zoom in
				   broadcastBallChaseCam.fov += 0.1f;
				}
				else if (keyboardState.IsKeyDown(Keys.Down))
				{
					//zoom out
					broadcastBallChaseCam.fov -= 0.1f;
				}
				//To be implemented
				if ((BallObject.IsSleeping || KBRedBall.Position.Y < -15f) && (keyboardState.IsKeyUp(Keys.Enter) && lastkeyboardState.IsKeyDown(Keys.Enter)))
				{
					//broadcastBallChaseCam.ResetCamera();
					FireBall();
				}
				else if ((keyboardState.IsKeyUp(Keys.Enter) && lastkeyboardState.IsKeyDown(Keys.Enter)))
				{
					PausePhysics = !PausePhysics;
				}
			}
		}
	}

}
